package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Identity;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.IdentityService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.PagesResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author hwj
 */
@RestController
@RequestMapping("/identity")
public class IdentityController {

    @Autowired
    private IdentityService identityService;

    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/pages")
    private PagesResult selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit)
    {
        IPage<Identity> identityIPage = identityService.selectIdentityPages(new Pages(page,limit));
        return new PagesResult(0,identityIPage.getRecords(),(int)identityIPage.getTotal(),identityIPage.getSize());
//        return new PagesResult(0,identityIPage.getRecords(),(int)identityIPage.getTotal(),identityIPage.getSize());
    }

    /**
     * 查询所有身份数据
     *
     * @date 2021/12/2
     */
    @RequestMapping("/queryList")
    public ApiResult queryList() {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,identityService.queryList());
    }


    /**
     * 查询所有身份数据
     *
     * @date 2021/12/2
     */
    @RequestMapping("/index")
    public ApiResult selectIdentityByUid(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(identityService.selectIdentityUid(uid)));
    }

    /**
     * 添加身份数据
     *
     * @param identity identity
     * @return 数据处理
     */
    @RequestMapping("/add")
    public ApiResult addIdentity(Identity identity) {
        return identityService.addIdentity(identity) ?  new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 删除身份数据
     *
     * @param uid uid
     * @return 数据返回
     */
    @RequestMapping("/delete")
    public ApiResult deleteIdentity(String uid) {
        return identityService.deleteIdentity(uid) ?  new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 修改身份数据
     *
     * @param identity 身份验证
     * @return 数据返回
     */
    @RequestMapping("/update")
    public ApiResult updateIdentity(Identity identity) {
        return identityService.updateIdentity(identity) ?  new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

    /**
     * 查询一个管理员所拥有的全部权限uid
     * 格式一般例如：101,102 逗号隔开
     *
     * @param admin_uid 管理员uid
     * @return 权限uid
     */
    @RequestMapping("/permissionUid")
    public ApiResult selectPermissionUid(String admin_uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,identityService.selectPermissionUid(admin_uid));

    }

    /**
     * 查询权限变更时间
     *
     * @param admin_uid 管理员
     * @return 时间
     */
    @RequestMapping("/updateTime")
    public ApiResult selectUpdateTime(String admin_uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,identityService.selectUpdateTime(admin_uid));

    }
}
